import Vue from 'vue'
export const commonObj = {
    checkLetAndNum(str) { //校验数字加字母
        var reg = new RegExp(/^(?![^a-zA-Z]+$)(?!\D+$)/);
        if (reg.test(str)) {
            return true;
        } else {
            return false;
        }
    },
    binaryInsertSort(arr, keyName, order) {
        for (var i = 1; i < arr.length; i++) {
            var key = arr[i],
                left = 0,
                right = i - 1;
            while (left <= right) {
                var middle = parseInt((left + right) / 2);
                if (order == "asc") {
                    if (parseFloat(key[keyName]) < parseFloat(arr[middle][keyName])) {
                        right = middle - 1;
                    } else {
                        left = middle + 1;
                    }
                } else {
                    if (parseFloat(key[keyName]) >= parseFloat(arr[middle][keyName])) {
                        right = middle - 1;
                    } else {
                        left = middle + 1;
                    }
                }

            }
            for (var j = i - 1; j >= left; j--) {
                arr[j + 1] = arr[j];
            }
            arr[left] = key;
        }
        return arr;
    },
    loadPubModule(){
        const requireComponent = require.context('@/components/',false,/\.vue$/);
        requireComponent.keys().forEach(fileName=>{
            const componentConfig = requireComponent(fileName);
            const componentName = capitalizeFirstLetter(fileName.replace(/^\.\//,'').replace(/\.\w+$/,''));
            Vue.component(componentName,componentConfig.default||componentConfig);
        })
        function capitalizeFirstLetter(string){
            return string.charAt(0).toUpperCase()+string.slice(1);
        }
    }
}